# MyDirectory.py program  - written by G. Rosa   10/21   ** Updated and Revised version 2.0
# Note: contains first name input feature - searches text file for matching first name.

import sys
import os

def start():
    global name
    name = ""
    name = input_data(name)
    print("")
    process(name)

def input_data(name):
    print("")
    name = ""
    name = input("      Enter First Name: ")
    print("")
    return name

def report(data,x):
    header = data[0].split(',')       # Note: data[0] set to zero in order to read header line.
    # print(header[0])                # 1st Header item on header line zero.

    line_item = data[x].split(',')
    # print(data[0].split(','))   # Line items with brackets, commas, end of line marker and quotation marks
    # print(data[0])              # line items with commas
    # print(line_item[0])         # First line item in specified line data[x]

    if name == line_item[0]:
        print("")
        # print("       " + "Name: " + line_item[0] + "  " + line_item[1])   # Combining First and Last Name here.
        print("       " + header[0] + "  " + line_item[0] + "       " + header[1] + "  " + line_item[1])
        print("")
        print("       " + header[2] + "  " + line_item[2])    # Prints street address.
        print("")
        print("       " + header[3] + "  " + line_item[3])    # Prints city.
        print("")
        print("       " + header[4] + "  " + line_item[4])    # Prints state.
        print("")
        print("       " + header[5] + "  " + line_item[5])    # Prints zip code.
        print("")
        print("       " + header[6] + "  " + line_item[6])    # Prints telephone number.
        print("")
        print("")
        key = input("      *** Enter 'Y' to do another search or hit Enter to exit ***     ")
        key = key.lower()
        if key == 'y':
            clear = lambda: os.system('cls')  # on Windows System
            clear()
            start()
        else:
            print("")
            sys.exit()
    elif x == len(data) - 1:
        print("           < First name not found >")
        print("")
        print("")
        key = input("      *** Enter 'Y' to try again or hit Enter to exit ***     ")
        key = key.lower()
        if key == 'y':
            clear = lambda: os.system('cls')  # on Windows System
            clear()
            start()
        else:
            print("")
            sys.exit()

def process(name):
        x = 0                # set to zero in order to read the header line first
        while x != len(data):
            # print(str(x) + "  " + str(len(data)))  # counter to follow iterations of 'x' & total # of lines in file.
            report(data,x)
            x += 1
        else:
            print("")

''' Program starts here '''

with open('c:/data/Directory.txt', 'r') as f:
    data = f.readlines()

global name_a
name = ""
print("")
name = input_data(name)
print("")
process(name)